var count = 0;
var tabIndex = 1;

$(document).ready(function(){
    $.cookie('tabs-view') == 'below' ? swap($('.tabbable').children('ul'), $('.tabbable').children('div'), 'after') : false;

	$('.content-view a').click(function(){
        setTabsView($(this));
    });

    $('#search').submit(function(){
        search($(this));
        return false;
    });
})

function fileManagerInit(options) {
    $().ready(function() {
        var elf = $('#elfinder').elfinder(options).elfinder('instance');
    });
}

function removeTab(obj) {
    var str = $(obj).parents('.myTab').children('a').attr('href');
    str = str.substring(1, str.length)
    $('.tabbable .tab-content #'+str).remove();
    $('.tabbable ul li[tab="'+str+'"]').remove();
    $('.tabbable .myTab a:first').tab('show');
}

function search(obj) {
    $(obj).find('button[type="submit"]').prop('disabled', true);
    var _query = $(obj).find('input[name="query"]').val();
    var _table = $(obj).find('input[name="table"]').val();
    var _model = $(obj).find('input[name="model"]').val();
    var _search_in = $(obj).find('input[name="search_in"]').val();
    var newTab, newTabPane = '';
    url = $(obj).attr('action')+$(obj).find('input[name="query"]').val();
    tabIndex == 1 ? tabIndex = $('.tab-right .tabbable ul li.myTab').size()-1 : tabIndex = tabIndex + 1;

    $.post(url, { table: _table, model: _model, search_in: _search_in }, function(data) {
        $(obj).find('button[type="submit"]').prop('disabled', false);

        if (data.status == 'error') { alert(data.data) }
        else {
            newTab = $('<li class="myTab" tab="tabSearch'+tabIndex+'"><a href="#tabSearch'+tabIndex+'" data-toggle="tab" onclick="hideForm()"><strong>'+_query+'</strong> - '+data.count+'<i class="icon-remove" style="margin-left: 10px" onclick="removeTab(this)"></i></a></li>');
            newTabPane = $('<div class="tab-pane" id="tabSearch'+tabIndex+'">'+data.data+'</div>')
            $('.tab-right .tabbable ul:first').append(newTab);
            $('.tabbable .tab-content').append(newTabPane);
            $('.tabbable a[href="#tabSearch'+tabIndex+'"]').tab('show');
        }
    },'JSON');
}

function setSearchState(obj) {
    var query = $(obj).parents('.input-prepend').children('input[name="query"]');
    query.attr('placeholder', $(obj).html());
    $(obj).parents('.input-prepend').children('input[name="search_in"]').val($(obj).attr('search_in'));
}

function swap(obj1, obj2, when) {
    when == 'after' ? obj1.insertAfter(obj2) : obj1.insertBefore(obj2);
}

function setTabsView(obj){
    var tabs = $(obj).parents('.tabbable').children('ul');
    var content = $(obj).parents('.tabbable').children('div');

    if ($(obj).attr('direction') == 'below') {
        swap(tabs, content, 'after');
    } else {
        swap(tabs, content, 'before');
    }

    $(obj).parents('.tabbable').removeClass().addClass('tabbable tabs-'+$(obj).attr('direction'));
    $.cookie('tabs-view', $(obj).attr('direction'));
}

function showForm(){
    //if (count == 0) {
        pluginsInit(function(){count = 1});
    /*} else {
        activateEditors();
    }*/
}

/*function hideForm(){
    deactivateEditors();
}

function deactivateEditors(){
    $('textarea.mceEditor').each(function(index){
        tinymce.EditorManager.execCommand('mceRemoveControl', true, $(this).attr('id'));
        //areas.push($(this).attr('id'));
    })
}

function activateEditors(){
    $('textarea.mceEditor').each(function(index){
        tinymce.EditorManager.execCommand('mceAddControl', true, $(this).attr('id'));
    })
}*/

function pluginsInit(callback) {
    $(function(){
        $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});
    })

    callback();
}

function sort(url, obj) {
	$(obj).parent().children('img').show();
    $.post(url, { value: $(obj).val() }, function(data) {
    	$(obj).parent().children('img').hide();
    	if (data.status == 'error') { alert(data.data) }
    });
}

function relationHasManyAdd(url, obj) {
	$(obj).prop('disabled', true);

    $.post(url, { owner: $(obj).attr('owner'), owned: $(obj).attr('owned'), owned_id: $(obj).parent().children('select').val(), owner_id: $(obj).attr('owner_id') }, function(data) {
    	$(obj).prop('disabled', false);
    	if (data.status == 'error') { alert(data.data) }
    });
}

function relationHasManyShow(url, obj) {
	$(obj).prop('disabled', true);

    $.post(url, { owner: $(obj).attr('owner'), owned: $(obj).attr('owned'), owner_id: $(obj).attr('owner_id'), waymark: $(obj).attr('waymark') }, function(data) {
    	$(obj).prop('disabled', false);
    	$(obj).parents('.controls').append(data);
        $(obj).parents('.controls').find('.well').show();
    	if (data.status == 'error') { alert(data.data) }
    });
}

function relationHasManyDel(url, obj) {
	var _owner = $(obj).parents('.well').find('input[name="owner"]').val();
	var _owned = $(obj).parents('.well').find('input[name="owned"]').val();
	var _owner_id = $(obj).parents('.well').find('input[name="owner_id"]').val();
	var _owned_id = $(obj).attr('owned_id');

    $.post(url, { owner: _owner, owned: _owned, owner_id: _owner_id, owned_id: _owned_id }, function(data) {
    	if (data.status == 'error') { alert(data.data) }
    	else {
    		$(obj).closest('tr').remove();
    	}
    });
}